package org.cloudie.docs.editor.client.content.dialogs;

import org.cloudie.docs.editor.client.widgets.ColorPicker;
import org.cloudie.docs.editor.client.widgets.Dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 * A dialog window displaying a selection of colors.
 */
public class SelectColorDialog extends Dialog {

  protected static SelectColorDialog instance;
  
  public static SelectColorDialog get() {
    if (instance == null) {
      instance = new SelectColorDialog();
    }
    return instance;
  }
  
  protected ColorPicker picker;
  protected SelectionHandler<String> handler;
	
  /**
   * Constructs a color selection dialog window.
   */
  protected SelectColorDialog() {
    super("Pick Color", true);
    mainPanel.getCellFormatter().setStyleName(2, 0, "");
    addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
		if (handler != null) {
	      ColorSelectionEvent e = new ColorSelectionEvent(null);
		  handler.onSelection(e);
		}
      }
    });
    HorizontalPanel content = new HorizontalPanel();
    picker = new ColorPicker();
    picker.addClickHandler(new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
		  hide();
		  if (handler != null) {
			ColorSelectionEvent e = new ColorSelectionEvent(picker.getSelectedColor());
			handler.onSelection(e);
		  }
		}
    });
    content.add(picker);
    setContentWidget(content);
  }
  
  public String getSelectedColor() {
	return picker.getSelectedColor();
  }
  
  public void center(SelectionHandler<String> handler) {
	super.center();
	this.handler = handler;
  }
  
  public class ColorSelectionEvent extends SelectionEvent<String> {

	public ColorSelectionEvent(String selectedItem) {
	  super(selectedItem);
	}
	
  }
}
